//
//  WYCBankCardController.m
//  初试
//
//  Created by 李胜军 on 2017/10/23.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCBankCardController.h"
#import "WYCCityModel.h"
#import "WYCprovinceModel.h"
#import "WYCPickerView.h"
#import <MJExtension.h>
#import "MyTextField.h"
#import <SVProgressHUD.h>
#import "RSAUtil.h"
#import "CustomPickerView.h"
#import "WYCbankPickerView.h"
#import "WYCBankModel.h"
#import "WYCRealnameController.h"

@interface WYCBankCardController ()
/** 电话*/
@property (strong , nonatomic) UITextField *mobileField;

/** 省按钮*/
@property (strong , nonatomic) UIButton * provincebutton;

/** 省View*/
@property (strong , nonatomic) UIView *provinceView;


/** 市View*/
@property (strong , nonatomic) UIView *cityView;


/** 省数组*/
@property (strong , nonatomic) NSMutableArray *provincearray;


/** 市数组*/
@property (strong , nonatomic) NSMutableArray *cityarray;

/** 选择器*/
@property (strong , nonatomic) UIPickerView *pickerView;


/** 市选择器*/
@property (strong , nonatomic) UIPickerView *citypickerView;


/** 省选择*/
@property (strong , nonatomic) UITextField *provinceField;



/** 省*/
@property (strong , nonatomic) UILabel * provincelabel;

/** 市选择*/
@property (strong , nonatomic) UITextField *cityField;

@property (nonatomic,strong) UITextField *sexField;


/** 市label*/
@property (strong , nonatomic) UILabel * citylabel;

/** name*/
@property (strong , nonatomic) NSString *provincename;
/** id*/
@property (strong , nonatomic) NSString *provinceID;


/** id*/
@property (strong , nonatomic) NSString *cityID;

/** 市模型*/
@property (strong , nonatomic) WYCprovinceModel *provinceModel;
/** 省市模型*/
@property (strong , nonatomic) WYCCityModel *CityModel;

@property (nonatomic, strong) WYCPickerView *addressView;

@property (nonatomic,strong) NSMutableArray *resultArray;


@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic,weak) CustomPickerView *provinceCustomPickerView;
@property (nonatomic,weak) CustomPickerView *CityCustomPickerView;

@property (nonatomic,weak)WYCbankPickerView  *bankPickerView;

/** 银行模型*/
@property(nonatomic,strong)WYCBankModel *bankModel;

/** 输入框*/
@property(nonatomic,strong)UITextField * cardtextField;
@end

@implementation WYCBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //界面
    [self setupNav];
    
    [self setupbanlist];
    //省数组初始化
    self.provincearray = [[NSMutableArray alloc] init];
    
    //市数组初始化
    self.cityarray = [[NSMutableArray alloc] init];
    
    //银行卡请求数组初始化
    self.resultArray = [[NSMutableArray alloc] init];
    
    // 省
    CustomPickerView *provinceCustomPickerView = [[CustomPickerView alloc]initWithSourceData:nil confirmBlock:^(WYCCityModel *cityModel) {
        
        [self.view endEditing:YES];
        self.provinceField.text = cityModel.name;
        self.provinceID = cityModel.ID;
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"id"] = cityModel.ID;
        WYCLog(@"----------%@",params);
        [HttpTool POST:AddressList params:params success:^(NSURLSessionDataTask *task, id responseObject) {
               WYCLog(@"----------%@",responseObject);
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                NSArray * dataArray = responseObject[@"result"];
                [self.cityarray removeAllObjects];
                for (NSDictionary * dict in dataArray) {
                    WYCprovinceModel * cityModel = [WYCprovinceModel mj_objectWithKeyValues:dict];
                    [self.cityarray addObject:cityModel];
                }
                [self.CityCustomPickerView refreshWithSourceData:self.cityarray];
                
            }else{
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                
            }
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    } cancelBlock:^(WYCCityModel *cityModel) {
        
        [self.view endEditing:YES];
        self.provinceField.text = @"";
    }];
    provinceCustomPickerView.frame = CGRectMake(0, 0, ScreenWidth, 280);
    self.provinceCustomPickerView = provinceCustomPickerView;
    self.provinceField.inputView = self.provinceCustomPickerView;

    
    
    
    
    // 市
    CustomPickerView *CityCustomPickerView = [[CustomPickerView alloc]initWithSourceData:nil confirmBlock:^(WYCCityModel *cityModel) {
        
        [self.view endEditing:YES];
        self.cityField.text = cityModel.name;
        self.cityID = cityModel.ID;
    } cancelBlock:^(WYCCityModel *cityModel) {
         [self.view endEditing:YES];
        self.cityField.text = @"";
    }];
     CityCustomPickerView.frame = CGRectMake(0, 0, ScreenWidth, 280);
    self.CityCustomPickerView = CityCustomPickerView;
    self.cityField.inputView = self.CityCustomPickerView;
    
    
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"id"] = @"0";
    [HttpTool POST:AddressList params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * dataArray = responseObject[@"result"];
            for (NSDictionary * dict in dataArray) {
                WYCCityModel * cityModel = [WYCCityModel mj_objectWithKeyValues:dict];
                [self.provincearray addObject:cityModel];
            }
            
            [self.provinceCustomPickerView refreshWithSourceData:self.provincearray];
            
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)setupbanlist{
    
    [HttpTool POST:BankList params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCBankModel * model = [WYCBankModel mj_objectWithKeyValues:dict];
                [self.resultArray addObject:model];
                self.bankModel = _bankModel;
                
            }
        }
        [self.bankPickerView refreshWithSourceData:self.resultArray];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    // 市
     WYCbankPickerView *bankPickerView = [[WYCbankPickerView alloc]initWithSourceData:self.resultArray confirmBlock:^(WYCBankModel *bankModel) {
         _bankModel = bankModel;
        [self.view endEditing:YES];
        self.sexField.text = bankModel.bankName;
         self.bankId = bankModel.bankId;
        
    } cancelBlock:^(WYCBankModel *bankModel) {
        [self.view endEditing:YES];
        self.sexField.text = @"";
    }];
    bankPickerView.frame = CGRectMake(0, 0, ScreenWidth, 280);
    self.bankPickerView = bankPickerView;
    self.sexField.inputView = self.bankPickerView;
}

//布局界面
- (void)setupNav {
    
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = WYCMain;
    [self.view addSubview:registerView];
    
    self.view.backgroundColor = WYCGlobalBg;
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 200 * px, 30, 400 * px, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"银行卡绑定";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, NavRigsterLabel.y, 80 * px , 80 * px);
    [registerView addSubview:backbutton];
    
    
    
    
    //照片
    UIImageView * iphoneimage = [[UIImageView alloc] init];
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.bankCardUrl]];
    iphoneimage.image = [UIImage imageWithData:data];
    iphoneimage.frame = CGRectMake(40 * px, CGRectGetMaxY(registerView.frame) + 40 * px, ScreenWidth - 80 * px, 600 * px);
    [self.view addSubview:iphoneimage];
    
    
    //姓名
    UILabel * nameLbel = [[UILabel alloc] init];
    nameLbel.text = @"银行卡号:";
    nameLbel.font = [UIFont systemFontOfSize:BigMiddleFont];
    nameLbel.textColor = WYCRGBColor(120, 112, 115);
    nameLbel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(iphoneimage.frame) + 80 * px, 300 * px, 60 * px);
    [self.view addSubview:nameLbel];
    
    
    //+1
    UITextField * nameField = [[UITextField alloc] init];
    nameField.frame = CGRectMake(CGRectGetMaxX(nameLbel.frame) + 20 * px, nameLbel.y, ScreenWidth, nameLbel.height);
    nameField.font = [UIFont systemFontOfSize:BigMiddleFont];
    nameField.text = self.cardNumber;
    nameField.keyboardType = UIKeyboardTypeNumberPad;
    nameField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:nameField];
    self.cardtextField = nameField;
    
    
    
    //开户银行
    UILabel * sexlabel = [[UILabel alloc] init];
    sexlabel.text = @"开户银行:";
    sexlabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    sexlabel.textColor = WYCRGBColor(120, 112, 115);
    sexlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(nameLbel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:sexlabel];
    
    
    //性别
    UITextField * sexField = [[UITextField alloc] init];
    sexField.frame = CGRectMake(CGRectGetMaxX(sexlabel.frame) + 20 * px, sexlabel.y, ScreenWidth, sexlabel.height);
    sexField.font = [UIFont systemFontOfSize:BigMiddleFont];
    
    sexField.text = [NSString stringWithFormat:@"%@",self.bankName];
    sexField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:sexField];
    self.sexField = sexField;
    
    //选择省
    UILabel * provincelabel = [[UILabel alloc] init];
    provincelabel.text = @"选择省:";
    provincelabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    provincelabel.textColor = WYCRGBColor(120, 112, 115);
    provincelabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(sexlabel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:provincelabel];
    self.provincelabel = provincelabel;
    
    
    
    _provinceField = [[UITextField alloc] init];
    _provinceField.placeholder = @"请选择省";
    _provinceField.frame = CGRectMake(CGRectGetMaxX(_provincelabel.frame) + 20 * px, _provincelabel.y, ScreenWidth, _provincelabel.height);
    _provinceField.font = [UIFont systemFontOfSize:BigMiddleFont];
//    _provinceField.backgroundColor = [UIColor whiteColor];
    //    _provinceField.enabled = NO;
    [self.view addSubview:_provinceField];
    
    
    //选择省
    UILabel * citylabel = [[UILabel alloc] init];
    citylabel.text = @"选择市:";
    citylabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    citylabel.textColor = WYCRGBColor(120, 112, 115);
    citylabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(provincelabel.frame) + 80 * px, nameLbel.width, 60 * px);
    [self.view addSubview:citylabel];
    self.citylabel = citylabel;
    
    
    
    _cityField = [[UITextField alloc] init];
    _cityField.placeholder = @"请选择市";
    _cityField.frame = CGRectMake(CGRectGetMaxX(citylabel.frame) + 20 * px, citylabel.y, ScreenWidth, citylabel.height);
    _cityField.font = [UIFont systemFontOfSize:BigMiddleFont];
//    _cityField.backgroundColor = [UIColor whiteColor];
    //    _provinceField.enabled = NO;
    [self.view addSubview:_cityField];
    

    
    //住址
    UILabel * Reservelabel = [[UILabel alloc] init];
    Reservelabel.text = @"预留手机号:";
    Reservelabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    Reservelabel.textColor = WYCRGBColor(120, 112, 115);
    Reservelabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(citylabel.frame) + 80 * px, nameLbel.width + 60 * px, 60 * px);
    [self.view addSubview:Reservelabel];
    
    
    //预留手机号
    MyTextField * mobileField = [[MyTextField alloc] init];
    mobileField.frame = CGRectMake(CGRectGetMaxX(Reservelabel.frame) + 20 * px, Reservelabel.y, ScreenWidth, Reservelabel.height);
    mobileField.font = [UIFont systemFontOfSize:BigMiddleFont];
    //    addressField.text = self.bankId;
    mobileField.keyboardType = UIKeyboardTypeNumberPad;
    mobileField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:mobileField];
    self.mobileField = mobileField;
    
    
    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(Reservelabel.frame) + 80 * px, ScreenWidth - iphoneimage.x * 2, 150 * px);
    loginButton.backgroundColor = WYCMain;
    loginButton.layer.cornerRadius = 10 * px;
    [loginButton setTitle:@"确认保存" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
}


//销毁控制器返回上一级
- (void)backClick{
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}
//保存
- (void)loginClick{
    
    
    if (self.mobileField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"预留手机号不能为空"];
        return;
    }
    if (_provinceField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"选择省不能为空"];
        return;
    }
    if (_cityField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"选择市不能为空"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    
    NSDictionary * jsonStr = @{@"userId":[WYCAccountTool unarchiveuserId],@"cardNumber":self.cardtextField.text,@"bankId":self.bankId,@"provinceId":self.provinceID,@"cityId":self.cityID,@"mobile":self.mobileField.text};
    
    WYCLog(@"jsonStr----%@",jsonStr);
    NSError *error;
    
    NSData * data = [NSJSONSerialization dataWithJSONObject:jsonStr options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSString *pas = [RSAUtil encryptString:jsonString publicKey:RSA_P_KEY];
   
    params[@"ticket"] = [WYCAccountTool unarchiveticket];
    params[@"request"] = pas;
    [HttpTool POST:saveDebitCard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        WYCLog(@"-----------%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"保存成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"card" object:nil];
            
            UIViewController *vc =self.presentingViewController;
            
            //ReadBookController要跳转的界面
            
            while (![vc isKindOfClass:[WYCRealnameController class]]) {
                
                
                
                vc = vc.presentingViewController;
                
            }
            
            [vc dismissViewControllerAnimated:YES completion:nil];
            
        }
        else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
    
    
}
//点击空白推荐盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.cardtextField resignFirstResponder];
    [self.mobileField resignFirstResponder];
}
@end

